from openai import OpenAI
from dotenv import load_dotenv
import os

load_dotenv()

client = OpenAI(
    api_key = os.getenv("API_KEY"), 
    base_url = os.getenv("API_URL")
)

print("开始流式调用LLM接口...")
print("=" * 50)

# 流式调用
response = client.chat.completions.create(
    model="Qwen2.5-72B-Instruct",
    messages=[
        {"role": "system", "content": "你是一个友好的AI助手"},
        {"role": "user", "content": "请详细介绍一下Python编程语言的特点和优势"}
    ],
    stream=True  # 启用流式响应
)

# 处理流式响应
print("AI助手回复：")
for chunk in response:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end='', flush=True)

print("\n" + "=" * 50)
print("流式响应完成！") 